﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dhsystem.Main;
using dhSystemBLL.produceBLL;
using baseSet.importExport;
using dhDataAcess.dataAcess;
using dhDataAcess.dataAcess.purchaseTableAdapters;
using dhsystem.produceForm.picking;
using baseSet.power;
namespace dhsystem.produceForm
{
     
    public partial class FrmrlckMaster : Form
    {
        rlckBLL rlckbll = new rlckBLL();
        string state;
        string stockName;
        public FrmrlckMaster(string _stockName)
        {
            InitializeComponent();
          stockName = _stockName;
        
        }
        //打开窗口时显示列表数据
        private void FrmrlckMaster_Load(object sender, EventArgs e)
        {
            getData();
        }
        //新增按钮，显示新增数据组件
        private void toolAdd_Click(object sender, EventArgs e)
        {
        state = "新增";
        FrmrlckMasterAdd rlckMasterAdd = new FrmrlckMasterAdd(bindingSourceMaster, state, stockName);
        rlckMasterAdd.ShowDialog();
        }
        //工具栏查询按钮，查询所有列表数据
        private void toolQuery_Click(object sender, EventArgs e)
        {
            FrmDataQuery query = new FrmDataQuery(moduleInfo.rlckMaster );
            DialogResult result = query.ShowDialog();
            if (result == DialogResult.Yes)
            {
               dGridDetail .DataSource  = query.rTable;
            }
        }
         //工具栏的编辑按钮,修改数据
        private void toolEdit_Click(object sender, EventArgs e)
        {
         state = "编辑";
         FrmrlckMasterAdd rlckMasterAdd = new FrmrlckMasterAdd(bindingSourceMaster, state, stockName);
         rlckMasterAdd.ShowDialog();
        }
        //删除按钮，删除选中行数据
        private void toolDel_Click(object sender, EventArgs e)
        {
          string billNo = dGridMaster.Rows[dGridMaster.CurrentRow.Index].Cells[1].Value.ToString();
          string billStatus = dGridMaster.CurrentRow.Cells["billStatus"].Value.ToString();
          if (billStatus == "结算")
          {
              MessageBox.Show("此订单已经结算，不能删除。如需删除请反结算");
              return;
          }
            try
            {
                DialogResult result = MessageBox.Show("是否删除：" + billNo + "！", "提示信息", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {

                    bindingSourceMaster.DataSource = rlckbll.rlckMasterDel(billNo);

                }
                getData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        //查询按钮，按条件查询数据
        private void buttonQuery_Click(object sender, EventArgs e)
        {
        
        }
        //刷新按钮
        private void buttupdate_Click(object sender, EventArgs e)
        {
           getData();
        }
         //工具栏退出按钮，退出窗口
        private void toolExit_Click(object sender, EventArgs e)
        {
          this.Close();
        }
           //datagridview第一列显示行号
        private void dGridMaster_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
        e.Row.HeaderCell.Value = string.Format("{0}", e.Row.Index + 1);
        }
        //datagridview第一列显示行号
        private void dGridDetail_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
          e.Row.HeaderCell.Value = string.Format("{0}", e.Row.Index + 1);
        }
         private void getData()
        {
            rlckDetailTableAdapter rlckDetailAdapter = new rlckDetailTableAdapter();
            rlckMasterTableAdapter rlckMasterAdapter = new rlckMasterTableAdapter();
            purchase rlck = new purchase();
            if (stockName == "原材料仓")
            {
                rlckMasterAdapter.FillrlckMasterByycl(rlck.rlckMaster);
            }
            else
            {
                rlckMasterAdapter.FillrlckMasterBywj (rlck.rlckMaster);
            }
            rlckDetailAdapter.FillrlckDetailALL (rlck.rlckDetail);

           
            bindingSourceMaster.DataSource = rlck;
            bindingSourceMaster.DataMember = "rlckMaster";
            dGridMaster.DataSource = bindingSourceMaster;

            DcolumnSet.colSet(moduleInfo.rlckMaster , dGridMaster);
            toolStripStatusLabel1.Text = "领料主表数据总条数:" + dGridMaster.RowCount.ToString() + "条";
            bindingSourceDetail.DataSource = bindingSourceMaster;

            bindingSourceDetail.DataMember = "fk_rlckDetail_billNo";
            dGridDetail.DataSource = bindingSourceDetail;
            DcolumnSet.colSet(moduleInfo.rlckDetail  , dGridDetail);
            toolStripStatusLabel3.Text = "领料从表数据总条数:" + dGridDetail.RowCount.ToString() + "条";
            bSourceNocheck.DataSource = rlckbll.getrlckMasterAllBybillstatus("新建");
            dGridNocheck.DataSource = bSourceNocheck;
            DcolumnSet.colSet(moduleInfo.rlckMaster, dGridNocheck);

            sysDataSet.modulAaccessDataTable modulAaccess = powerSet.modulPower(moduleInfo.rlckMaster );
            foreach (sysDataSet.modulAaccessRow modulAaccessRow in modulAaccess)
            {
                buttonQuery.Enabled = modulAaccessRow.showAc;
                toolAdd.Enabled = modulAaccessRow.addAc;
                toolEdit.Enabled = modulAaccessRow.editAc;
                toolDel.Enabled = modulAaccessRow.delAc;
                toolPrint.Enabled = modulAaccessRow.printAc;
                toolExport.Enabled = modulAaccessRow.exportAc;
                //buttImport.Enabled = modulAaccessRow.importAc;

            }
        }

         private void toolExport_Click(object sender, EventArgs e)
         {
             FrmExcelExport frm = new FrmExcelExport(moduleInfo.rlckMaster , dGridMaster , "领料出库信息");
             frm.ShowDialog();
         }

         private void 领料出库单终结ToolStripMenuItem_Click(object sender, EventArgs e)
         {
             string billNo = dGridNocheck.Rows[dGridNocheck.CurrentRow.Index].Cells[1].Value.ToString();
             Frmrlckcheck check = new Frmrlckcheck(bindingSourceMaster , billNo);
             check.ShowDialog();
         }

         private void button2_Click(object sender, EventArgs e)
         {
             bSourceNocheck.DataSource = rlckbll.getrlckMasterAllBybillstatus("新建");
             dGridNocheck.DataSource = bSourceNocheck;
             DcolumnSet.colSet(moduleInfo.rlckMaster, dGridNocheck);
         }

    
    }
}
